<?php
/*  Show Country Flag
 *  Plugin FOR Discuz! X2 ML
 *	Copyright (c) 2012-2012
 *	$Id: fl_show_country_flag.class.php V1.0 UPDATE 20120328 SINGCEE $
 */

if(!defined('IN_DISCUZ')) {
     exit('Access Denied');
}

class plugin_fl_show_country_flag {
     function plugin_fl_show_country_flag() {
          global $_G;
          $this->showflaghideid = $_G['cache']['plugin']['fl_show_country_flag']['showflaghideid'];
          $this->showflagstyle = $_G['cache']['plugin']['fl_show_country_flag']['showflagstyle'];
     }
}
class plugin_fl_show_country_flag_forum extends plugin_fl_show_country_flag {
	function fl_show_country_flag_code($ip='') {
		require_once(DISCUZ_ROOT.'./source/function/geoip.inc');
		$gi = geoip_open(DISCUZ_ROOT.'./data/ipdata/GeoIP.dat',GEOIP_STANDARD);
		$country = geoip_country_code_by_addr($gi, $ip);
		geoip_close($gi);
		if($country) {
			return $country;
		}else{
			return '??';
		}
	}

	function fl_show_country_flag_go() {
	 $flreturn = array();
	 global $_G,$postlist;
	 require_once libfile('function/misc');
	 if(empty($postlist) || !is_array($postlist)) return $flreturn;
	 if(!empty($this->showflaghideid)){
	 	 $hideidarr=explode(",",$this->showflaghideid);
	 }
	 foreach ($postlist as $pid => $post) {
	 	 if(!in_array($post['authorid'], $hideidarr)) {
	 	 	 $useip = DB::fetch_first("SELECT `useip` FROM ".DB::table('forum_post')." WHERE `pid`='".$pid."'");
	 	 	 $country=$this->fl_show_country_flag_code($useip['useip']);
	 	 	 $cfile=strtolower($country).'.gif';
	 	 	 if(!is_file('source/plugin/fl_show_country_flag/img/'.$cfile)){
	 	 	 	 $cfile='loading.gif';
	 	 	 }
	 	 	 if($this->showflagstyle){
		 	 	 $flag_country=lang('country',$country);
		 	 	 $flag_img='<img src="source/plugin/fl_show_country_flag/img/'.$cfile.'" title="'.$flag_country.'" style="padding-top: 5px" />';
		 	 	 $resstr=$this->showflagstyle;
		 	 	 $resstr=eregi_replace('\$flag_img', $flag_img, $resstr);
		 	 	 $resstr=eregi_replace('\$flag_country', $flag_country, $resstr);
		 	 	 $flreturn[]=$resstr;
	 	 	 }else{
	 	 	 	$flreturn[]='<div style="margin-left: 16px; width: 131px; overflow: hidden; height:22px;background: transparent url(source/plugin/fl_show_country_flag/flag_gb.gif);">&nbsp;<img src="source/plugin/fl_show_country_flag/img/'.$cfile.'" title="'.lang('country',$country).'" style="padding-top: 5px" />&nbsp;'.lang('country',$country).'</div>';
	 	 	 }
	 	 }else{
	 	 	 $flreturn[]='';
	 	 }
	 }
	 return $flreturn;
	}
	function viewthread_sidebottom_output() {
	  	return $this->fl_show_country_flag_go();
	}
}
?>